import CardFunctions from "../Common/CardFunctions";
import { GameData, Levels } from "../Data/GameData";
import Grid from "../Game/Grid";
import Board from "../Game/Board";
import { LevelData_1001 } from "../Data/Level/LevelData_1001";
import { LevelData_1002 } from "../Data/Level/LevelData_1002";

const { ccclass, property, executionOrder } = cc._decorator;

/** MapManager */
@ccclass
// 慢一点执行
@executionOrder(102)
export default class MapManager extends cc.Component {
    static Instance: MapManager;

    onLoad() {
        MapManager.Instance = this;
    }

    initCard() {
        this.node.destroyAllChildren();
        GameData.arrCards = [];
        GameData.arrGrids = [];
        this.node.width = GameData.W + 0; // this.cardWidth * this.column + 20;
        this.node.height = GameData.H + 0; // this.cardHeight * this.row + 20;

        for (let x = 0; x < GameData.col; x++) {
            GameData.arrGrids[x] = [];
            for (let y = 0; y < GameData.row; y++) {
                const id = 0;
                const scrGrid: Grid = CardFunctions.CreateGrid(id, x, y);
                GameData.arrGrids[x][y] = scrGrid;
            }
        }

        this.InitBoard();

        let b = CardFunctions.GetAllBlockGroup();

        this.InitCreateBlockGroup();
    }

    InitCreateBlockGroup() {
        for (let index = 0; index < 3; index++) {
            CardFunctions.随机创建一个BlockGroup(index);
        }
    }

    InitBoard() {
        let levelData = Levels[GameData.CurrentLevel].u; //LevelData_1001;
        console.log("levelData", levelData);

        // let levelData = LevelData_1002;
        for (let index = 0; index < levelData.length; index++) {
            const layer_data = levelData[index];
            console.log("layer_data", layer_data);


            for (let index2 = 0; index2 < layer_data.length; index2++) {
                const board_data = layer_data[index2];
                console.log("board_data", board_data);

                const x = board_data.x;
                const y = board_data.y;
                const layer = index;
                const _layout = board_data.l;
                const _cardList = board_data.c;
                const board: Board = CardFunctions.CreateBoard(x, y, layer, _layout, _cardList);
            }
        }

        // CardFunctions.RefreshAllCardCover(GameData.MapData_CurrentLevel);
        CardFunctions.SetAllBoardCover();
    }

    // InitCard() {
    //     // let currentLevelDataList = GameData.CurrentLevelDataList[GameData.CurrentLevel];
    //     // GameData.Card_types = currentLevelDataList.word; //CommonFunction.pickRandom(arr, 6) //itemdata_DateMapData.card_types[GameData.CurrentLevel];

    //     // GameData.MapData_CurrentLevel = [];

    //     // this.InitBoard();
    // }

    // InitMap() {
    //     CardFunctions.Shuffle_MapData_CurrentLevel();
    //     // console.log(`GameData.MapData_CurrentLevel`, GameData.MapData_CurrentLevel);

    //     for (let index = 0; index < GameData.MapData_CurrentLevel.length; index++) {
    //         const item_MapData = GameData.MapData_CurrentLevel[index];
    //         item_MapData.cardGameState = CardGameState.游戏场景;
    //         item_MapData.list_index = index;
    //         // console.log(item_MapData);

    //         const node: cc.Node = cc.instantiate(AssetsManager.Prefabs.Card);
    //         // console.log(`node`, node);

    //         node.active = true;
    //         const card: Card = node.getComponent(Card);
    //         // card.Init(_index);
    //         const card_type = item_MapData.card_type;
    //         const card_layer = item_MapData.card_layer;
    //         const card_pos = item_MapData.card_pos;
    //         card.card_layer = card_layer;
    //         // console.log(`card_type`, card_type);
    //         card.node.parent = GameData.LayerList[card_layer - 1];
    //         const x = card_pos.x * GameData.card_width;
    //         const y = card_pos.y * (GameData.card_height);
    //         // console.log(x, y);
    //         card.node.setPosition(cc.v2(x, y));
    //         card.node.width = GameData.card_width;
    //         card.node.height = GameData.card_height;
    //         card.list_index = index;

    //         let isCover = CardFunctions.GetCardCover(item_MapData, GameData.MapData_CurrentLevel);
    //         card.isCover = isCover;

    //         item_MapData.oldInfo =
    //         {
    //             card_pos: card_pos,
    //             cardGameState: item_MapData.cardGameState,
    //         }

    //         let color = CardFunctions.GetRandomColor();
    //         card.Init(x, y, color);
    //         // card.Init(x, y, card_type);

    //         GameData.arrCards.push(card);
    //     }

    //     CardFunctions.RefreshAllCardCover(GameData.MapData_CurrentLevel);
    // }

    // InitCreateCard() {
    //     for (let index = 0; index < 8; index++) {
    //         let x = index;
    //         let y = 0;
    //         let color = CardFunctions.GetRandomColor();
    //         // color = 1;
    //         CardFunctions.CreateCard(x, y, color);
    //     }
    // }

    // InitLayer() {
    //     CommonFunction.SetAllChildrenAtive(UIManager.Instance.LayerNode, false);
    //     // 层级初始化
    //     for (let index = 0; index < GameData.Leve_l_layer_count; index++) {
    //         const element = GameData.Leve_l_layer_count[index];
    //         const child = CommonFunction.GetFreeChild(UIManager.Instance.LayerNode);
    //         child.active = true;
    //     }
    //     GameData.LayerList = UIManager.Instance.LayerNode.children;
    //     // console.log(`this.LayerList.length`, this.LayerList.length);
    // }
}

/** 调试 */
window["MapManager"] = MapManager;